<?php
namespace app\vueapi\controller;

use app\common\model\{
    Laboratory as LaboratoryModel , College as CollegeModel , Semester as SemesterModel
};

use think\Db;

class Laboratory extends Base
{
    public function index()
    {
        $data = $this->request->param();
        $page = $data['pageSize'] ?? 10;
        if(!empty($data['name'])){
            $res = LaboratoryModel::where('name',$data['name'])->find()->paginate($page);
            return json(msg(1, $res));
        }else {
            $res = LaboratoryModel::find()->paginate($page);
            return json(msg(1, $res));
        }
    }

    public function index1()
    {
        $data = $this->request->param();
        $page = $data['pageSize'] ?? 10;
        $res1 = LaboratoryModel::find()->select();
        $res['data'] = $res1;
        return json(msg(1, $res));
    }

    public function seek()
    {
        $data = $this->request->param();
        $page = $data['pageSize'] ?? 100;
        if(!empty($data['name']) || !empty($data['adress']) || !empty($data['laboratory']) || !empty($data['phone']) || !empty($data['teacher'])){
            
            if(!empty($data['name'])) {
                $res = CollegeModel::where('name',$data['name'])->find();
                $whereOr = [
                    'college_id'=> $res['id'],
                    'adress' => $data['adress'],
                    'name' => $data['laboratory'],
                    'phone' => $data['phone'],
                    'teacher' => $data['teacher']
                ];
            }else {
                $whereOr = [
                    'adress' => $data['adress'],
                    'name' => $data['laboratory'],
                    'phone' => $data['phone'],
                    'teacher' => $data['teacher']
                ];
            }
            
        }else{
            return json(msg(0, '', '请输入查询值'));
        }
        $res = LaboratoryModel::whereOr($whereOr) -> paginate($page);
        return json(msg(1, $res));
    }

    public function seek_college_id()
    {
        $data = $this->request->param();

        $res = LaboratoryModel::where('college_id',$data['college_id'])->select();
        return json(msg(1, $res));
    }

    public function addLaboratory()
    {
        $data = $this->request->param();
        if(!empty($data['name']) || !empty($data['adress']) || !empty($data['area']) || !empty($data['facility']) || !empty($data['facility_money']) || !empty($data['teacher']) || !empty($data['phone'])){
            $res = LaboratoryModel::where('name',$data['name'])->select();
            if($res != []) {
                return json(msg(0, '', '该实验室已存在！'));
            }else{
                $res = LaboratoryModel::insert($data);
                $name = LaboratoryModel::where('name',$data['name'])->find();
                $data1['laboratory_id'] = $name['id'];
                $res1 = SemesterModel::insert($data1);
                return returnRes($name,'添加'); 
            }
        }else {
            return json(msg(0, '', '值不能为空'));
        }
    }

    public function delLaboratory()
    {
        $data = $this->request->param();
        //不允许有相同的属性
        $college = LaboratoryModel::where('id', $data['id'])->find();
        if(empty($college)){
            return json(msg(0, '', '该实验室不存在'));
        }
        $res = $college->delete();
        return returnRes($res, '删除实验室');
    }

    public function changeLaboratory()
    {
        $data = $this->request->param();
        //不允许有相同的属性
        $laboratory = LaboratoryModel::where('id', $data['id'])->find();
        if(!empty($data['name']) || !empty($data['adress']) || !empty($data['area']) || !empty($data['facility']) || !empty($data['facility_money']) || !empty($data['teacher']) || !empty($data['phone'])){
            $laboratory->name  = $data['name'];
            $laboratory->adress  = $data['adress'];
            $laboratory->area  = $data['area'];
            $laboratory->facility  = $data['facility'];
            $laboratory->facility_money  = $data['facility_money'];
            $laboratory->teacher  = $data['teacher'];
            $laboratory->phone  = $data['phone'];
            $laboratory -> save();
            return returnRes($laboratory, '修改');
        }
        return json(msg(0, '', '该实验室不存在'));
    }
}

?>